package com.openlogic.common.logic.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openlogic.common.core.exception.IllegalExpressionException;
import com.openlogic.common.core.exception.logic.RuleFuncParamNumberIllegalArgument;
import com.openlogic.common.core.exception.logic.RuleFuncParamTypeIllegalArgument;
import com.openlogic.common.core.exception.logic.RuleFuncParamUnKnowException;
import com.openlogic.common.core.expression.Element;
import com.openlogic.common.core.expression.Element.ElementType;
import com.openlogic.common.core.expression.ExpressionUtil;
import com.openlogic.common.core.utils.ConvertUtil;
import com.openlogic.common.logic.func.enums.FuncLibMethodsEnums;

/**
* @ClassName: ExpreUtils
* @Description: 规则公式校验工具类
* @author weiyongpeng
* @date 2024年7月26日 下午2:51:54
 */
public class FuncLibExpreUtils {

	private static final Logger logger = LoggerFactory.getLogger(FuncLibExpreUtils.class);
	
	public static Map<String, Integer> operators = new HashMap<String, Integer>();

	private static final String SPLITOR = "(),";

	private static final String NUMBER_CHARS = "01234567890.";
	
	private static final String VARIABLE_CHARS = "@";

	static {
		operators.put("*", 2);
		operators.put("/", 2);

		operators.put("+", 3);
		operators.put("-", 3);

		operators.put("=", 4);
		operators.put("!=", 4);
		operators.put(">", 4);
		operators.put(">=", 4);
		operators.put("<", 4);
		operators.put("<=", 4);
		operators.put("等于", 4);
		operators.put("不等于", 4);
		operators.put("大于", 4);
		operators.put("大于等于", 4);
		operators.put("小于", 4);
		operators.put("小于等于", 4);

		operators.put("并且", 5);
		operators.put("或者", 5);
		operators.put("AND", 5);
		operators.put("OR", 5);

		operators.put("包含", 5);
		operators.put("不包含", 5);
		operators.put("IN", 5);
		operators.put("NOT IN", 5);
		operators.put("匹配", 5);
		operators.put("不匹配", 5);
		operators.put("LIKE", 5);
		operators.put("NOT LIKE", 5);

	}

	/**
	* 描述：将公式拆分为数组，为转化后缀表达式用
	* @Title: split
	* @param expr
	* @return
	* @throws IllegalExpressionException
	* @author weiyongpeng
	 * @param map 
	* @date  2024年7月26日 下午3:33:02
	 */
	public static List<Element> split(String expr, List<Map<String, Object>> list) throws IllegalExpressionException {
		List<Element> elements = new ArrayList<Element>();
		int len = expr.length();
		String noAnalyze = null;
		int nai = 0;
		for (int i = 0; i < len;) {
			String ce = expr.substring(i);
			if (ce.startsWith("#")) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				String var = "";
				int j = 1;
				for (; j < len - i; j++) {
					var += ce.charAt(j);
					if (ce.charAt(j) == '#') {
						break;
					}
				}
				if (j == len - i) {
					throw new IllegalExpressionException("#" + var, "dataColumnClose", i);
				}
				elements.add(new Element("#" + var, ElementType.DATACOLUMN, i));
				i += j + 1;
			} else if ("'".equals(Character.toString(ce.charAt(0)))) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				String var = "";
				int j = 1;
				for (; j < len - i; j++) {
					var += ce.charAt(j);
					if (ce.charAt(j) == '\'') {
						break;
					}
				}
				if (j == len - i) {
					throw new IllegalExpressionException("'" + var, "stringClose", i);
				}
				elements.add(new Element("'" + var, ElementType.STRING, i));
				i += j + 1;
			} else if (ce.startsWith("NOT LIKE")) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				for (int j = ((8 < len - i) ? 8 : len - i); j >= 0; j--) {
					if (operators.containsKey(ce.substring(0, j))) {
						i += j;
						elements.add(new Element(ce.substring(0, j), ElementType.OPERATOR, i));
						break;
					}
				}
			} else if (ce.startsWith("NOT IN")) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				for (int j = ((6 < len - i) ? 6 : len - i); j >= 0; j--) {
					if (operators.containsKey(ce.substring(0, j))) {
						i += j;
						elements.add(new Element(ce.substring(0, j), ElementType.OPERATOR, i));
						break;
					}
				}
			} else if (ce.startsWith("小于等于") || ce.startsWith("大于等于") || ce.startsWith("LIKE")) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				for (int j = ((4 < len - i) ? 4 : len - i); j >= 0; j--) {
					if (operators.containsKey(ce.substring(0, j))) {
						i += j;
						elements.add(new Element(ce.substring(0, j), ElementType.OPERATOR, i));
						break;
					}
				}
			} else if (ce.startsWith("不等于") || ce.startsWith("不匹配") || ce.startsWith("不包含") || ce.startsWith("AND")) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				for (int j = ((3 < len - i) ? 3 : len - i); j >= 0; j--) {
					if (operators.containsKey(ce.substring(0, j))) {
						i += j;
						elements.add(new Element(ce.substring(0, j), ElementType.OPERATOR, i));
						break;
					}
				}
			} else if (ce.startsWith("<=") || ce.startsWith(">=") || ce.startsWith("等于") || ce.startsWith("大于")
					|| ce.startsWith("小于") || ce.startsWith("并且") || ce.startsWith("或者") || ce.startsWith("OR")
					|| ce.startsWith("包含") || ce.startsWith("IN") || ce.startsWith("匹配")) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				for (int j = ((2 < len - i) ? 2 : len - i); j >= 0; j--) {
					if (operators.containsKey(ce.substring(0, j))) {
						i += j;
						elements.add(new Element(ce.substring(0, j), ElementType.OPERATOR, i));
						break;
					}
				}
			} else if (ce.startsWith("+") || ce.startsWith("-") || ce.startsWith("*") || ce.startsWith("/")
					|| ce.startsWith("=") || ce.startsWith(">") || ce.startsWith("<")) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				if(ce.substring(0,1).equals("-") && NumericRegxUtils.isNumber(ce.substring(1,ce.length()-1))) {
					elements.add(new Element(ce.substring(0,1), ElementType.NUMBER, i));
					i += 1;
					continue;
				}
				for (int j = ((1 < len - i) ? 1 : len - i); j >= 0; j--) {
					if (operators.containsKey(ce.substring(0, j))) {
						i += j;
						elements.add(new Element(ce.substring(0, j), ElementType.OPERATOR, i));
						break;
					}
				}
			} else if (ce.startsWith("!=")) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				for (int j = ((1 < len - i) ? 2 : len - i); j >= 0; j--) {
					if (operators.containsKey(ce.substring(0, j))) {
						i += j;
						elements.add(new Element(ce.substring(0, j), ElementType.OPERATOR, i));
						break;
					}
				}
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_CONCAT.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_CONCAT.getCode(), ElementType.FUNCTION, nai));
				i += 17;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_CONCATENAT.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_CONCATENAT.getCode(), ElementType.FUNCTION, nai));
				i += 21;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_LEFTSTR.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_LEFTSTR.getCode(), ElementType.FUNCTION, nai));
				i += 18;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_RIGHTSTR.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_RIGHTSTR.getCode(), ElementType.FUNCTION, nai));
				i += 19;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_LOWER.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_LOWER.getCode(), ElementType.FUNCTION, nai));
				i += 16;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_UPPER.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_UPPER.getCode(), ElementType.FUNCTION, nai));
				i += 16;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_REPLACE.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_REPLACE.getCode(), ElementType.FUNCTION, nai));
				i += 18;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_REPLACEPLUS.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_REPLACEPLUS.getCode(), ElementType.FUNCTION, nai));
				i += 22;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_LEN.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_LEN.getCode(), ElementType.FUNCTION, nai));
				i += 14;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STRING_REVERSE.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STRING_REVERSE.getCode(), ElementType.FUNCTION, nai));
				i += 18;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_COLLECTION_ADD.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_COLLECTION_ADD.getCode(), ElementType.FUNCTION, nai));
				i += 15;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_COLLECTION_REMOVE.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_COLLECTION_REMOVE.getCode(), ElementType.FUNCTION, nai));
				i += 25;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_COLLECTION_CONCAT.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_COLLECTION_CONCAT.getCode(), ElementType.FUNCTION, nai));
				i += 18;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_COLLECTION_DISTINCT.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_COLLECTION_DISTINCT.getCode(), ElementType.FUNCTION, nai));
				i += 20;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_COLLECTION_CONTAINS.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_COLLECTION_CONTAINS.getCode(), ElementType.FUNCTION, nai));
				i += 20;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_COLLECTION_NOTCONTAINS.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_COLLECTION_NOTCONTAINS.getCode(), ElementType.FUNCTION, nai));
				i += 23;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_COLLECTION_TO_LIST.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_COLLECTION_TO_LIST.getCode(), ElementType.FUNCTION, nai));
				i += 18;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_COLLECTION_TO_SET.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_COLLECTION_TO_SET.getCode(), ElementType.FUNCTION, nai));
				i += 17;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_COLLECTION_TO_MAP.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_COLLECTION_TO_MAP.getCode(), ElementType.FUNCTION, nai));
				i += 17;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STATIS_MEDIAN.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STATIS_MEDIAN.getCode(), ElementType.FUNCTION, nai));
				i += 17;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STATIS_STANDARD_DIVIATION.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STATIS_STANDARD_DIVIATION.getCode(), ElementType.FUNCTION, nai));
				i += 31;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STATIS_POPULATION_VARIANCE.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STATIS_POPULATION_VARIANCE.getCode(), ElementType.FUNCTION, nai));
				i += 32;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_STATIS_VARIANCE.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_STATIS_VARIANCE.getCode(), ElementType.FUNCTION, nai));
				i += 22;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_ADD.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_ADD.getCode(), ElementType.FUNCTION, nai));
				i += 12;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_SUB.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_SUB.getCode(), ElementType.FUNCTION, nai));
				i += 12;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_DIV.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_DIV.getCode(), ElementType.FUNCTION, nai));
				i += 12;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_DIVBYSCALE.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_DIVBYSCALE.getCode(), ElementType.FUNCTION, nai));
				i += 19;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_MUL.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_MUL.getCode(), ElementType.FUNCTION, nai));
				i += 12;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_ROUND.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_ROUND.getCode(), ElementType.FUNCTION, nai));
				i += 14;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_ROUNDUP.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_ROUNDUP.getCode(), ElementType.FUNCTION, nai));
				i += 16;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_ROUNDDOWN.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_ROUNDDOWN.getCode(), ElementType.FUNCTION, nai));
				i += 18;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_AVERAGE.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_AVERAGE.getCode(), ElementType.FUNCTION, nai));
				i += 16;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.CUS_MATH_MOD.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.CUS_MATH_MOD.getCode(), ElementType.FUNCTION, nai));
				i += 12;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_ABS.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_ABS.getCode(), ElementType.FUNCTION, nai));
				i += 8;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_ROUND.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_ROUND.getCode(), ElementType.FUNCTION, nai));
				i += 10;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_FLOOR.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_FLOOR.getCode(), ElementType.FUNCTION, nai));
				i += 10;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_CEIL.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_CEIL.getCode(), ElementType.FUNCTION, nai));
				i += 9;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_SQRT.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_SQRT.getCode(), ElementType.FUNCTION, nai));
				i += 9;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_POW.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_POW.getCode(), ElementType.FUNCTION, nai));
				i += 8;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_LOG.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_LOG.getCode(), ElementType.FUNCTION, nai));
				i += 8;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_LOG10.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_LOG10.getCode(), ElementType.FUNCTION, nai));
				i += 10;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_SIN.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_SIN.getCode(), ElementType.FUNCTION, nai));
				i += 8;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_COS.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_COS.getCode(), ElementType.FUNCTION, nai));
				i += 8;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_TAN.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_TAN.getCode(), ElementType.FUNCTION, nai));
				i += 8;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_ATAN.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_ATAN.getCode(), ElementType.FUNCTION, nai));
				i += 9;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_ACOS.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_ACOS.getCode(), ElementType.FUNCTION, nai));
				i += 9;
			} else if (ce.startsWith(StringUtils.join(FuncLibMethodsEnums.AVIATOR_MATH_ASIN.getCode(),"("))) {
				elements.add(new Element(FuncLibMethodsEnums.AVIATOR_MATH_ASIN.getCode(), ElementType.FUNCTION, nai));
				i += 9;
			} else if (ce.startsWith(" ") || ce.startsWith("\t") || ce.startsWith("\n") || ce.startsWith("\r")) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				i++;
				elements.add(new Element(" ", ElementType.SEPARATOR, i));
			} else if (NUMBER_CHARS.contains(Character.toString(ce.charAt(0)))) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				String number = "";
				int j = 0;
				for (; j < len - i; j++) {
					if (!NUMBER_CHARS.contains(Character.toString(ce.charAt(j)))) {
						break;
					}
					number += Character.toString(ce.charAt(j));
				}
				elements.add(new Element(number, ElementType.NUMBER, i));
				i += j;
			} else if (Character.toString(ce.charAt(0)).startsWith(VARIABLE_CHARS)) { // 变量
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				String number = "";
				String type = ""; // 变量类型
				int j = 0;
				fo:
				for (; j < len - i; j++) {
					number += Character.toString(ce.charAt(j));
					if(list.size() > 0) {
						for (Map<String, Object> map : list) {
							for (Map.Entry<String, Object> entry : map.entrySet()) {
								if (!number.replaceAll(VARIABLE_CHARS, "").contains(entry.getValue().toString()) || entry.getValue().toString().equals("sql")) {
									continue;
								}else {
									type = map.get("type").toString();
									break fo;
								}
							}
						}
					}else {
						logger.error("变量未知类型");
						throw new RuleFuncParamUnKnowException("不明确的变量异常");
					}
				}
				if(type.equals("2") || type.equals("4")) { // 数字型
					elements.add(new Element(number.replaceAll(VARIABLE_CHARS, ""), ElementType.NUMBER, i));
				}else if(type.equals("1")){	// 字符型
					elements.add(new Element(number.replaceAll(VARIABLE_CHARS, ""), ElementType.STRING, i));
				}else if(type.equals("sql")) { 
					elements.add(new Element(number + VARIABLE_CHARS, ElementType.STRING, i));
				}else if(type.equals("9")) { // sql参数类型
					elements.add(new Element(number + VARIABLE_CHARS, ElementType.STRING, i));
				}
				i += j+1;
			} else if (SPLITOR.contains(Character.toString(ce.charAt(0)))) {
				if (noAnalyze != null) {
					elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
					noAnalyze = null;
				}
				elements.add(new Element(Character.toString(ce.charAt(0)), ElementType.SPLITOR, i));
				i++;
			} else {
				if (noAnalyze == null) {
					nai = i;
				}
				noAnalyze = (noAnalyze == null ? "" : noAnalyze) + Character.toString(ce.charAt(0));
				i++;
			}
		}
		if (noAnalyze != null) {
			elements.add(new Element(noAnalyze, ElementType.NOANALYZE, nai));
			noAnalyze = null;
		}
		return elements;
	}

	/**
	* 描述：将拆分的公式中缀表达式转化为后缀表达式，并校验
	* @Title: compile
	* @param splitExprs
	* @return
	* @throws IllegalExpressionException
	* @author weiyongpeng
	* @date  2024年7月26日 下午3:04:45
	 */
	public static Map<String, Object> compile(List<Element> splitExprs) throws IllegalExpressionException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 负符号的问题和NOANALYZE类型
		List<Element> eles = new ArrayList<Element>();
		String operator = null;
		int operatorCount = 0;
		// 将解析的列表进行类型转化
		for (int i = 0; i < splitExprs.size(); i++) {
			if (Element.ElementType.NOANALYZE == splitExprs.get(i).getType()) {
				throw new IllegalExpressionException(splitExprs.get(i).getText(), "noanalyze",
						splitExprs.get(i).getStartIndex());
			}
			if (splitExprs.get(i).getType() == ElementType.OPERATOR && "-".equals(splitExprs.get(i).getText())
					&& i < splitExprs.size() && splitExprs.get(i + 1).getType() == ElementType.NUMBER
					&& (i == 0 || splitExprs.get(i - 1).getType() == ElementType.OPERATOR
							|| splitExprs.get(i - 1).getType() == ElementType.SPLITOR
							|| splitExprs.get(i - 1).getType() == ElementType.SEPARATOR)) {
				operator = splitExprs.get(i).getText();
				operatorCount += 1;
			}
			Element e = splitExprs.get(i);
			if (e.getType() == ElementType.OPERATOR && operatorCount < 0) {
				e.setText(operator + e.getText());
				operator = null;
			}
			eles.add(e);
		}
		List<Element> _RPNExpList = new ArrayList<Element>(); 	// 后缀表达式队列
		Stack<Element> opStack = new Stack<Element>();			// 操作队列
		Stack<Element> verifyStack = new Stack<Element>();		// 校验队列
		List<List<Element>> funcs = new ArrayList<List<Element>>();
		Element _function = null;
		for (Element ele : eles) {
			if (Element.ElementType.DATACOLUMN == ele.getType()) {
				_RPNExpList.add(ele);
				verifyStack.push(ele);
			} else if (Element.ElementType.NUMBER == ele.getType()) {
				_RPNExpList.add(ele);
				verifyStack.push(ele);
			} else if (Element.ElementType.NOANALYZE == ele.getType()) {
				verifyStack.push(ele);
			} else if (Element.ElementType.SPECIALPARAM == ele.getType()) {
				_RPNExpList.add(ele);
				verifyStack.push(ele);
			} else if (Element.ElementType.STRING == ele.getType()) {
				_RPNExpList.add(ele);
				verifyStack.push(ele);
			} else if (Element.ElementType.OPERATOR == ele.getType()) {
				if (opStack.empty()) {
					opStack.push(ele);
				} else {
					boolean doPeek = true;
					while (doPeek & !opStack.empty()) {
						Element topEle = opStack.peek();
						if (Element.ElementType.OPERATOR == topEle.getType()) {
							if (operators.get(topEle.getText()) > operators.get(ele.getText())) {
								opStack.push(ele);
								doPeek = false;
							} else if (operators.get(topEle.getText()).equals(operators.get(ele.getText()))) {
								Element verityEle = verifyOperator(topEle, verifyStack);
								verifyStack.push(verityEle);
								opStack.pop();
								_RPNExpList.add(topEle);
							} else {
								Element verityEle = verifyOperator(topEle, verifyStack);
								verifyStack.push(verityEle);
								opStack.pop();
								_RPNExpList.add(topEle);
							}
						} else if (Element.ElementType.FUNCTION == topEle.getType()) {
							opStack.push(ele);
							doPeek = false;
						} else if (Element.ElementType.SPLITOR == topEle.getType()) {
							opStack.push(ele);
							doPeek = false;
						}
					}
					if (doPeek && opStack.empty()) {
						opStack.push(ele);
					}
				}
			} else if (Element.ElementType.SPLITOR == ele.getType()) {
				if ("(".equals(ele.getText())) {
					// 函数的左括号
					if (_function != null) {
						verifyStack.push(ele);
						_RPNExpList.add(ele);
						// 将"("及临时缓存的函数压入操作符栈,括号在前
						opStack.push(ele);
						opStack.push(_function);
						_function = null;
					} else {
						opStack.add(ele);
					}
				} else if (")".equals(ele.getText())) {
					boolean doPop = true;
					while (doPop && !opStack.empty()) {
						Element topEle = opStack.pop();
						if (Element.ElementType.OPERATOR == topEle.getType()) {
							Element result = verifyOperator(topEle, verifyStack);
							verifyStack.push(result);
							_RPNExpList.add(topEle);
						} else if (Element.ElementType.FUNCTION == topEle.getType()) {
							Element result = verifyFunction(topEle, verifyStack, funcs);
							verifyStack.push(result);
							_RPNExpList.add(ele);
							_RPNExpList.add(topEle);
						} else if ("(".equals(topEle.getText())) {
							doPop = false;
						}
					}
					if (doPop && opStack.empty()) {
						throw new IllegalExpressionException(ele.getText(), "leftBracketLack", ele.getStartIndex());
					}
				} else if (",".equals(ele.getText())) {
					boolean doPeek = true;
					while (doPeek && !opStack.empty()) {
						Element topEle = opStack.peek();
						if (Element.ElementType.FUNCTION == topEle.getType()) {
							doPeek = false;

							_RPNExpList.add(ele);
						} else if (Element.ElementType.OPERATOR == topEle.getType()) {
							opStack.pop();
							Element result = verifyOperator(topEle, verifyStack);
							verifyStack.push(result);
							_RPNExpList.add(topEle);
						} else if (Element.ElementType.SPLITOR == topEle.getType()) {
							throw new IllegalExpressionException(ele.getText(), "comma", ele.getStartIndex());
						}
					}
				}
			} else if (Element.ElementType.FUNCTION == ele.getType()) {
				_function = ele;
			}
		}
		// 将操作栈内剩余的操作符放入逆波兰式队列
		while (!opStack.empty()) {
			Element topEle = opStack.pop();
			if (Element.ElementType.OPERATOR == topEle.getType()) {
				// 执行操作符校验
				Element result = verifyOperator(topEle, verifyStack);
				// 把校验结果压入检验栈
				verifyStack.push(result);
				_RPNExpList.add(topEle);
			} else if (Element.ElementType.FUNCTION == topEle.getType()) {
				throw new IllegalExpressionException(topEle.getText(), "functionClose", topEle.getStartIndex());
			} else if ("(".equals(topEle.getText())) {
				throw new IllegalExpressionException(topEle.getText(), "rightBracketLack", topEle.getStartIndex());
			}
		}

		if (verifyStack.size() != 1) {
			StringBuffer errorBuffer = new StringBuffer("");
			int position = -1;
			while (!verifyStack.empty()) {
				Element topEle = verifyStack.pop();
				errorBuffer.append("'").append(topEle.toString()).append("',");
				if (position == -1) {
					position = topEle.getStartIndex();
				}
			}
			throw new IllegalExpressionException(errorBuffer.toString(), "operatorlack", position);
		} else {
			Element e = verifyStack.pop();
			resultMap.put("result", e);
		}
		resultMap.put("funcs", funcs);
		resultMap.put("rpn", _RPNExpList);
		return resultMap;
	}

	/**
	* 描述：验证操作符以及操作数是否合规
	* @Title: verifyOperator
	* @param ele
	* @param verifyStack
	* @return
	* @throws IllegalExpressionException
	* @author weiyongpeng
	* @date  2024年7月26日 下午3:04:17
	 */
	private static Element verifyOperator(Element ele, Stack<Element> verifyStack) throws IllegalExpressionException {
		List<Element> args = new ArrayList<Element>();
		String t = "";
		for (int i = 0; i < 2; i++) {
			if (!verifyStack.empty()) {
				Element e = verifyStack.pop();
				args.add(e);
				if (operators.get(ele.getText()) <= 4) {
					if (Element.ElementType.NUMBER != e.getType() && Element.ElementType.SPECIALPARAM != e.getType()
							&& Element.ElementType.DATACOLUMN != e.getType()) {
						throw new IllegalExpressionException(ele.getText(), "operatorType", ele.getStartIndex());
					}
				} else {
					if (Element.ElementType.BOOLEAN != e.getType()) {
						throw new IllegalExpressionException(ele.getText(), "operatorType", ele.getStartIndex());
					}
				}
			} else {
				throw new IllegalExpressionException(ele.getText(), "argCount", ele.getStartIndex());
			}
		}
		t = args.get(1).getText() + ele.getText() + args.get(0).getText();
		if (operators.get(ele.getText()) >= 4) {
			return new Element(t, ElementType.BOOLEAN, ele.getStartIndex());
		} else {
			return new Element(t, ElementType.NUMBER, ele.getStartIndex());
		}
	}
	
	/**
	* 描述：验证函数是否合规 
	* @Title: verifyFunction
	* @param ele
	* @param verifyStack
	* @param funcs
	* @return
	* @throws IllegalExpressionException
	* @author weiyongpeng
	* @date  2024年7月26日 下午3:03:43
	 */
	private static Element verifyFunction(Element ele, Stack<Element> verifyStack, List<List<Element>> funcs)
			throws IllegalExpressionException {
		List<Element> funcArg = new ArrayList<Element>();
		if (!verifyStack.empty()) {
			List<Element> args = new ArrayList<Element>();
			boolean doPop = true;
			while (doPop && !verifyStack.empty()) {
				Element e = verifyStack.pop();
				funcArg.add(e);
				if ("(".equals(e.getText())) {
					doPop = false;
				} else {
					args.add(e);
				}
			}
			if (FuncLibMethodsEnums.CUS_MATH_ADD.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_MATH_SUB.getCode().equals(ele.getText())
					|| FuncLibMethodsEnums.CUS_MATH_MUL.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_MATH_DIV.getCode().equals(ele.getText())
					|| FuncLibMethodsEnums.CUS_MATH_MOD.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_MATH_ROUND.getCode().equals(ele.getText())
					|| FuncLibMethodsEnums.CUS_MATH_ROUNDUP.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_MATH_ROUNDDOWN.getCode().equals(ele.getText())) {
				if (args.size() != 2) { // add函数参数为2，且都为数字
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				for (int i = 0; i < args.size(); i++) {
					if(Element.ElementType.NUMBER != args.get(i).getType()) {
						logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
						throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
					}
				}
			} else if (FuncLibMethodsEnums.CUS_MATH_AVERAGE.getCode().equals(ele.getText())) {
				if (args.size() < 0) { // add函数参数为2，且都为数字
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				for (int i = 0; i < args.size(); i++) {
					if(Element.ElementType.NUMBER != args.get(i).getType()) {
						logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
						throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
					}
				}
			} else if (FuncLibMethodsEnums.CUS_STRING_CONCATENAT.getCode().equals(ele.getText())) {
				if (args.size() < 3) { // add函数参数为2，且都为数字
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				for (int i = 0; i < args.size(); i++) {
					if(Element.ElementType.STRING != args.get(i).getType()) {
						logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
						throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
					}
				}
			} else if (FuncLibMethodsEnums.CUS_STRING_LEFTSTR.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_STRING_RIGHTSTR.getCode().equals(ele.getText())) {
				if (args.size() != 2) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				if(Element.ElementType.STRING != args.get(0).getType()) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}else if(Element.ElementType.NUMBER != args.get(1).getType()) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_STRING_REPLACE.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_STRING_RIGHTSTR.getCode().equals(ele.getText())) {
				if (args.size() != 2) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				for (int i = 0; i < args.size(); i++) {
					if(Element.ElementType.STRING != args.get(i).getType()) {
						logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
						throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
					}
				}
			} else if (FuncLibMethodsEnums.CUS_STRING_REPLACEPLUS.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_STRING_RIGHTSTR.getCode().equals(ele.getText())) {
				if (args.size() != 4) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				if(Element.ElementType.STRING != args.get(0).getType() || Element.ElementType.STRING != args.get(3).getType()) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}else if(Element.ElementType.NUMBER != args.get(1).getType() || Element.ElementType.NUMBER != args.get(2).getType()) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_STRING_LOWER.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_STRING_UPPER.getCode().equals(ele.getText())
					|| FuncLibMethodsEnums.CUS_STRING_REVERSE.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_STRING_LEN.getCode().equals(ele.getText())) {
				if (args.size() != 1) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				if(Element.ElementType.STRING != args.get(0).getType()) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_STRING_CONCAT.getCode().equals(ele.getText())) {
				if (args.size() < 2) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				for (int i = 0; i < args.size(); i++) {
					if(Element.ElementType.STRING != args.get(i).getType()) {
						logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
						throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
					}
				}
			} else if (FuncLibMethodsEnums.CUS_COLLECTION_CONTAINS.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_COLLECTION_NOTCONTAINS.getCode().equals(ele.getText())) {
				if (args.size() != 2) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				if (!args.get(1).getText().contains("@") && !args.get(1).getText().contains("toList") && !args.get(1).getText().contains("toSet")) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_COLLECTION_TO_LIST.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_COLLECTION_TO_SET.getCode().equals(ele.getText())) {
				if (args.size() == 0) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_COLLECTION_TO_MAP.getCode().equals(ele.getText())) {
				if (args.size() == 0 || args.size() % 2 != 0) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_COLLECTION_ADD.getCode().equals(ele.getText())) {
				if (args.size() != 2) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				if (!args.get(1).getText().contains("@") && !args.get(1).getText().contains("toList") && !args.get(1).getText().contains("toSet")) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_COLLECTION_REMOVE.getCode().equals(ele.getText())) {
				if (args.size() != 2) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				if (!args.get(1).getText().contains("@") && !args.get(1).getText().contains("toList") && !args.get(1).getText().contains("toSet")) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_COLLECTION_CONCAT.getCode().equals(ele.getText())) {
				if (args.size() != 2) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				long count = args.stream().filter(eles -> !eles.getText().contains("@") && !eles.getText().contains("toList") && !eles.getText().contains("toSet")).count();
				if (count > 0) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_COLLECTION_DISTINCT.getCode().equals(ele.getText())) {
				if (args.size() != 1) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				long count = args.stream().filter(eles -> !eles.getText().contains("@") && !eles.getText().contains("toList") && !eles.getText().contains("toSet")).count();
				if (count > 0) {
					logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
				}
			} else if (FuncLibMethodsEnums.CUS_STATIS_VARIANCE.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_STATIS_POPULATION_VARIANCE.getCode().equals(ele.getText())
					|| FuncLibMethodsEnums.CUS_STATIS_MEDIAN.getCode().equals(ele.getText()) || FuncLibMethodsEnums.CUS_STATIS_STANDARD_DIVIATION.getCode().equals(ele.getText())) {
				if (args.size() == 1 || args.size() <= 0) { 
					logger.error("公式参数个数异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
					throw new RuleFuncParamNumberIllegalArgument("公式参数个数校验异常");
				}
				for (int i = 0; i < args.size(); i++) {
					if(Element.ElementType.NUMBER != args.get(i).getType()) {
						logger.error("公式参数类型异常：{}，异常位置：{}",ele.getText(), ele.getStartIndex());
						throw new RuleFuncParamTypeIllegalArgument("公式参数类型校验异常");
					}
				}
			}
		} else {
			throw new IllegalExpressionException(ele.getText(), "functionFull", ele.getStartIndex());
		}
		funcArg.add(ele);
		funcs.add(funcArg);
		return new Element(ele.getText(), ElementType.NUMBER, ele.getStartIndex());
	}

	/**
	* 描述：公式解析校验
	* @Title: validateRuleFunc
	* @param res
	* @param error
	* @param ruleResovle
	* @param result
	* @return
	* @author weiyongpeng
	* @date  2024年7月26日 下午3:31:52
	 */
	public static List<Map<String, Object>> validateRuleFunc(Map<String, Object> res, Map<String, Object> error,
			StringBuilder ruleResovle, Map<String, Object> result) {
		List<Element> _rpnList = ConvertUtil.objToList(result.get("rpn"), Element.class);
		if (result.get("result") != null
				&& Element.ElementType.NUMBER != ((Element) result.get("result")).getType()
				&& Element.ElementType.STRING != ((Element) result.get("result")).getType()) {
			error.put("errorText", "exprerror");
			error.put("errorType", "");
		} else {
			Stack<Element> complieStack = new Stack<Element>();
			for (Element ele : _rpnList) {
				if (ele.getType() == ElementType.NUMBER || ele.getType() == ElementType.STRING) {
					complieStack.push(ele);
				} else if (ele.getType() == ElementType.OPERATOR) {
					if("并且".equals(ele.getText())){
						ele.setText("AND");
					}else if("或者".equals(ele.getText())){
						ele.setText("OR");
					}
					String txt = complieStack.pop().getText();
					txt = "(" + complieStack.pop().getText() + " " +ele.getText()+" " + txt + ")";
					complieStack.push(new Element(txt, ExpressionUtil.operators.get(ele.getText()) > 3
							? ElementType.BOOLEAN : ElementType.NUMBER, ele.getStartIndex()));
				} else if (ele.getType() == ElementType.FUNCTION) {
					List<Element> argsList = new ArrayList<Element>();
					Element popEle = complieStack.pop();
					argsList.add(popEle);
					if (")".equals(popEle.getText())) {
						boolean doPop = true;
						while (doPop && !complieStack.empty()) {
							Element parameter = complieStack.pop();
							if ("(".equals(parameter.getText())) {
								doPop = false;
							}
							argsList.add(parameter);
						}
					}
					argsList.add(ele);
					String txt = "";
					if (ele.getText().startsWith(FuncLibMethodsEnums.CUS_MATH.getCode()) 
							|| ele.getText().startsWith(FuncLibMethodsEnums.CUS_STRING.getCode())) {
						String errorTxt = "";
						boolean isError = false;
						for (int i = argsList.size() - 1; i >= 0; i--) {
							txt += argsList.get(i).getText();
							errorTxt += argsList.get(i).getText();
						}
						if (isError) {
							error.put("errorText", errorTxt);
							error.put("errorType", "targetnoexist");
						}
					} else {
						for (int i = argsList.size() - 1; i >= 0; i--) {
							txt += argsList.get(i).getText();
						}
					}
					complieStack.push(new Element(txt, ElementType.NUMBER, ele.getStartIndex()));
				} else if (ele.getType() == ElementType.SPLITOR) {
					complieStack.push(ele);
				} 
			}
			ruleResovle.append(complieStack.get(0).getText());
		}
		List<Map<String,Object>> resultList = new ArrayList<>();
		if (error.size() > 0) {
			res.put("retCode", "0");
			res.put("error", error);
			resultList.add(res);
		} else {
			res.put("retCode", "1");
			res.put("ruleResolve", ruleResovle.toString().replaceAll("<", "&lt")); //小于号在页面展示需要转义
			resultList.add(res);
		}
		return resultList;
	}
}
